package com.syh.jn.scm.service.impl.downstream.finance;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.event.annotation.DomainEventHandle;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.finance.DownCostBill;
import com.syh.jn.scm.domain.downstream.entity.finance.DownStatementAccountBill;
import com.syh.jn.scm.domain.downstream.event.DownCostBillEvent;
import com.syh.jn.scm.domain.downstream.event.DownStatementAccountBillEvent;
import com.syh.jn.scm.domain.downstream.repository.finance.DownCostBillRepository;
import com.syh.jn.scm.dto.downstream.finance.DownCostBillVo;
import com.syh.jn.scm.dto.support.FilesDto;
import com.syh.jn.scm.service.api.downstream.finance.DownCostBillService;
import com.syh.jn.scm.service.api.downstream.finance.enums.DownCostBillEnum;
import com.syh.jn.scm.service.api.downstream.finance.enums.DownStatementAccountBillEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Date:2018/7/19
 * Author:Clown
 * 下游费用单service实现;
 */
@Service
@Transactional
public class DownCostBillServiceImpl extends AbstractServiceImpl<Object, Object, DownCostBillVo, DownCostBill, Long> implements DownCostBillService {
  @Autowired
  private DownCostBillRepository downstreamCostBillRepositoryImpl;

  @Override
  protected AggregateRepository<DownCostBill, Long> getAggregateRepository() {
    return downstreamCostBillRepositoryImpl;
  }

  //确认订单生效
  @Override
  public Boolean confirmState(Long id) {
    DownCostBill downCostBill = downstreamCostBillRepositoryImpl.getById(id);
    downCostBill.confirmState();
    downstreamCostBillRepositoryImpl.update(downCostBill);

    //发送事件消息(生成应收付记录)
    //双方确认之后
    if (downCostBill.getState().equals(DownCostBillEnum.CONFIRM.getStatus()) && downCostBill.getConfirmState().equals(DownCostBillEnum.OTHERPARTY_CONFIRM.getStatus())) {
      this.publicEvent(DownCostBillEvent.buildDownCostBillStateConfirmEvent(new Object[]{downCostBill}));
    }
    return Boolean.TRUE;
  }

  //取消确认订单生效
  @Override
  public Boolean cancelConfirm(Long id) {
    DownCostBill downCostBill = downstreamCostBillRepositoryImpl.getById(id);
    downCostBill.cancelConfirm();
    downstreamCostBillRepositoryImpl.update(downCostBill);
    //服务商确认的时候
    if (downCostBill.getState().equals(DownCostBillEnum.CONFIRM.getStatus())) {
      this.publicEvent(DownCostBillEvent.buildDownCostBillStateUnConfirmEvent(new Object[]{downCostBill}));
    }
    return Boolean.TRUE;
  }

  /**
   * 上传单据图片
   *
   * @param dto
   */
  @Override
  public void upload(FilesDto dto) {
    DownCostBill bill = downstreamCostBillRepositoryImpl.getById(dto.getId());
    bill.upload(dto);
    downstreamCostBillRepositoryImpl.upload(bill);
  }

  /**
   * 消费者：监听对账单（生成对账单状态和对账单号）
   *
   * @param bill
   */
  @DomainEventHandle(DownStatementAccountBillEvent.DOWN_STATEMENT_BILL_ACCOUNT_CREATED)
  protected void handleDownStatementBillAccountCreated(DownStatementAccountBill bill) {
    bill.getDownStatementAccountBillItems().forEach(item -> {
      if (item.getSourceType() == DownStatementAccountBillEnum.SOURCE_COST_BILL.getStatus()) {
        DownCostBill costBill = downstreamCostBillRepositoryImpl.getById(item.getSourceId());
        costBill.generating(bill.getId());
        downstreamCostBillRepositoryImpl.upload(costBill);
      }
    });
  }

  /**
   * 消费者：监听对账单（修改对账单状态）
   *
   * @param bill
   */
  @DomainEventHandle(DownStatementAccountBillEvent.DOWN_STATEMENT_BILL_ACCOUNT_UPDATA)
  protected void handleDownStatementBillAccountUpdata(DownStatementAccountBill bill) {
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("statementOfAccountId", bill.getId()));
    List<DownCostBill> downCostBills = downstreamCostBillRepositoryImpl.getList(wrapper);
    downCostBills.forEach(downCostBill -> {
      downCostBill.cancelGenerating();
      downstreamCostBillRepositoryImpl.upload(downCostBill);
    });
    bill.getDownStatementAccountBillItems().forEach(item -> {
      if (item.getSourceType() == DownStatementAccountBillEnum.SOURCE_COST_BILL.getStatus()) {
        DownCostBill costBill = downstreamCostBillRepositoryImpl.getById(item.getSourceId());
        costBill.generating(bill.getId());
        downstreamCostBillRepositoryImpl.upload(costBill);
      }
    });
  }

  /**
   * 消费者：监听对账单（删除对账单状态和对账单号）
   *
   * @param bill
   */
  @DomainEventHandle(DownStatementAccountBillEvent.DOWN_STATEMENT_BILL_ACCOUNT_DESTORY)
  protected void handleDownStatementBillAccountDestory(DownStatementAccountBill bill) {
    bill.getDownStatementAccountBillItems().forEach(item -> {
      if (item.getSourceType() == DownStatementAccountBillEnum.SOURCE_COST_BILL.getStatus()) {
        DownCostBill costBill = downstreamCostBillRepositoryImpl.getById(item.getSourceId());
        costBill.cancelGenerating();
        downstreamCostBillRepositoryImpl.upload(costBill);
      }
    });
  }
}
